package com.zy.baselib.widget.input

import android.text.InputFilter
import android.text.Spanned
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * @author: shalei
 * @since: 2020-01-14 09:11
 * @Desc:
 **/
class InputFilterPhone : InputFilter {

    override fun filter(
        source: CharSequence?,
        start: Int,
        end: Int,
        dest: Spanned?,
        dstart: Int,
        dend: Int
    ): CharSequence {
        if(source == null) return ""
        if(stringFilterNumber(source)) {
            return source
        }
        return ""
    }

    private fun stringFilterNumber(source: CharSequence): Boolean {
        val regEx = "[0-9]*"
        val p: Pattern = Pattern.compile(regEx)
        val m: Matcher = p.matcher(source)
        return !m.find()
    }
}